package com.glsc.ngateway.opmanage.service;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.Visit;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.TInterfaceAccessHistoryTableRepository;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.VisitRepo;
import com.glsc.ngateway.opmanage.utils.SpecificationFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Title: MetaFlowServiceImpl
 * @Description: 流程元数据类
 * @author: 张宁
 * @version: 1.0.0
 * @date: 2020/12/21 16:29
 */
@Service
public class StatisticsService {


    @Autowired
    private VisitRepo visitRepo;

    @Autowired
    private TInterfaceAccessHistoryTableRepository accessRepo;

    // TInterfaceAccessHistoryTable 相关查询
    public Integer statModuleByDate(String module, String beginDate, String endDate) {

        return accessRepo.statModuleByDate(module, beginDate, endDate);
    }

    // TInterfaceAccessHistoryTable 相关查询
    public Integer statOAByDate(String beginDate, String endDate) {

        return accessRepo.statOAByDate(beginDate, endDate);
    }

    // t_ldap_user_visit_flow 相关查询
    public List<Map> countByDate(String date) {
        return visitRepo.countByDate(getDate(date));
    }


    public List<Map> countByUser(String sysName, String date) {
        return visitRepo.countByUser(sysName, getDate(date));
    }

    public String getDate(String date) {
        if (date.equals("")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            date = sdf.format(new Date());
        } else
            date = date.replaceAll("-", "");
        return date;
    }


    public List<Map> countByMonth(String month) {
        return visitRepo.countByMonth(getMonth(month));
    }


    public List<Map> countMonthBySys(String sysName, String month) {
        return visitRepo.countMonthBySys(sysName, getMonth(month));
    }


    public String getMonth(String month) {
        if (month.equals("")) {
            Calendar cal = Calendar.getInstance();
            int yyyy = cal.get(Calendar.YEAR);
            int MM = cal.get(Calendar.MONTH) + 1;
            month = yyyy + (MM < 10 ? "0" : "") + MM;
        } else {
            month = month.replaceAll("-", "");
            if (month.length() == 5) {
                StringBuffer sb = new StringBuffer(month);
                month = sb.insert(4, "0").toString();
            }
        }
        return month;
    }

    public Map<String, List<Long>> countByYear(String year) {
        if (year.equals("")) {
            Calendar cal = Calendar.getInstance();
            year = String.valueOf(cal.get(Calendar.YEAR));
        }
        List<String> sys = visitRepo.findAllSys();
        String[] months = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        Map<String, List<Long>> res = new HashMap<>();
        for (String s : sys) {
            List<Long> list = new ArrayList<>();
            for (String m : months) {
                Specification spec = SpecificationFactory.build(SpecificationFactory.OP.AND, new SpecificationFactory.SpecElement(SpecificationFactory.Type.LIKE, "accessDate", year + m), new SpecificationFactory.SpecElement(SpecificationFactory.Type.EQ, "systemId", s));
                list.add(visitRepo.count(spec));
            }
            res.put(s, list);
        }
        return res;
    }


    public Page<Visit> getLoginData(Integer page, Integer size) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "expireTime";
        Pageable pageable = PageRequest.of(page - 1, size, direction, sort);

        Page<Visit> res = visitRepo.findUserData(pageable);

        return res;
//        return new PageImpl<>(res.getContent(),pageable,res.getTotalElements());
    }


}
